package com.the4man.cnbdw.listener.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * Created by lenovo on 2017/12/12.
 */
public class SocketThread extends Thread {
    private ServerSocket serverSocket = null;

    public SocketThread(ServerSocket serverScoket) {
        try {
            if(this.serverSocket == null) {
                this.serverSocket = new ServerSocket(8889);
                System.out.println("socket start");
            }
        } catch (Exception var3) {
            System.out.println("SocketThread创建socket服务出错");
            var3.printStackTrace();
        }

    }

    public void run() {
        while(!this.isInterrupted()) {
            try {
                Socket socket = this.serverSocket.accept();
                System.out.println("--:" + (new Date()).toLocaleString());
                if(socket != null && !socket.isClosed()) {
                    (new SocketOperate(socket)).start();
                    System.out.println("1");
                } else {
                    System.out.println("2");
                }

                socket.setSoTimeout(30000);
            } catch (Exception var2) {
                var2.printStackTrace();
            }
        }

    }

    public void closeSocketServer() {
        try {
            if(this.serverSocket != null && !this.serverSocket.isClosed()) {
                this.serverSocket.close();
            }
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }
}
